# dir declaration
LIB_SECURESHIELD_DIR = $(LIBRARIES_ROOT)/secureshield

LIB_SECURESHIELD_ASMSRCDIR	= $(LIB_SECURESHIELD_DIR)/core/src $(LIB_SECURESHIELD_DIR)/core/src/arc_hal
LIB_SECURESHIELD_CSRCDIR	= $(LIB_SECURESHIELD_DIR)/core/src $(LIB_SECURESHIELD_DIR)/core/src/arc_hal
LIB_SECURESHIELD_INCDIR		= $(EMBARC_ROOT)/arc

# include board related makefile
include $(LIB_SECURESHIELD_DIR)/board/$(VALID_BOARD)/$(VALID_BOARD).mk

ifeq ($(LIB_SECURESHIELD_VERSION), 1)

LIB_SECURESHIELD_ASMSRCDIR	+= $(LIB_SECURESHIELD_DIR)/core/src/v1
LIB_SECURESHIELD_CSRCDIR	+= $(LIB_SECURESHIELD_DIR)/core/src/v1
LIB_SECURESHIELD_INCDIR		+= $(LIB_SECURESHIELD_DIR)/core/src/v1

else

ifeq ($(LIB_SECURESHIELD_VERSION), 2)

LIB_SECURESHIELD_USE_MPU_SID ?= 0
LIB_SECURESHIELD_DEFINES += -DSECURESHIELD_USE_MPU_SID=$(LIB_SECURESHIELD_USE_MPU_SID)

LIB_SECURESHIELD_ASMSRCDIR	+= $(LIB_SECURESHIELD_DIR)/core/src/v2
LIB_SECURESHIELD_CSRCDIR	+= $(LIB_SECURESHIELD_DIR)/core/src/v2
LIB_SECURESHIELD_INCDIR		+= $(LIB_SECURESHIELD_DIR)/core/src/v2

endif

endif

LIB_SECURESHIELD_INCDIR		+= $(LIB_SECURESHIELD_DIR)/core/inc

# USE_SECURESHIELD_APPL_GEN is from embarc cli, which indicate the application configuration generator works success
ifeq ($(USE_SECURESHIELD_APPL_GEN), 1)
LIB_SECURESHIELD_AUTOGEN_DIR = $(APPL_CSRC_DIR)
SECURESHIELD_NORMAL_LINKER_SCRIPT_FILE  ?= $(LIB_SECURESHIELD_AUTOGEN_DIR)/linker_normal_temp.ld
SECURESHIELD_SECUR_LINKER_SCRIPT_FILE ?= $(LIB_SECURESHIELD_AUTOGEN_DIR)/linker_secure_temp.ld
else
ifeq ($(VALID_TOOLCHAIN), mw)
SECURESHIELD_NORMAL_LINKER_SCRIPT_FILE  ?= $(LIB_SECURESHIELD_DIR)/board/secureshield_normal_template_mw.ld
SECURESHIELD_SECUR_LINKER_SCRIPT_FILE ?= $(LIB_SECURESHIELD_DIR)/board/secureshield_secure_template_mw.ld
else
SECURESHIELD_NORMAL_LINKER_SCRIPT_FILE ?= $(LIB_SECURESHIELD_DIR)/board/secureshield_normal_template_gnu.ld
SECURESHIELD_SECUR_LINKER_SCRIPT_FILE ?= $(LIB_SECURESHIELD_DIR)/board/secureshield_secure_template_gnu.ld
endif
endif

override LINKER_SCRIPT_FILE  = $(SECURESHIELD_NORMAL_LINKER_SCRIPT_FILE)


# the dir to generate objs
LIB_SECURESHIELD_OBJDIR = $(OUT_DIR)/library/secureshield

# find all the srcs in the target dirs
LIB_SECURESHIELD_CSRCS = $(call get_csrcs, $(LIB_SECURESHIELD_CSRCDIR))
LIB_SECURESHIELD_ASMSRCS = $(call get_asmsrcs, $(LIB_SECURESHIELD_ASMSRCDIR))

# get object files
LIB_SECURESHIELD_COBJS = $(call get_relobjs, $(LIB_SECURESHIELD_CSRCS))
LIB_SECURESHIELD_ASMOBJS = $(call get_relobjs, $(LIB_SECURESHIELD_ASMSRCS))
LIB_SECURESHIELD_OBJS = $(LIB_SECURESHIELD_COBJS) $(LIB_SECURESHIELD_ASMOBJS)

# get dependency files
LIB_SECURESHIELD_DEPS = $(call get_deps, $(LIB_SECURESHIELD_OBJS))


APPL_SECURE_CSRCS += $(call get_csrcs, $(APPL_SECURE_CSRC_DIR))
APPL_SECURE_ASMSRCS += $(call get_asmsrcs, $(APPL_SECURE_ASMSRC_DIR))
APPL_SECURE_CXXSRCS += $(call get_cxxsrcs, $(APPL_SECURE_CXXSRC_DIR))
APPL_SECURE_COBJS += $(call get_appobjs, $(APPL_SECURE_CSRCS))
APPL_SECURE_ASMOBJS += $(call get_appobjs, $(APPL_SECURE_ASMSRCS))
APPL_SECURE_CXXOBJS += $(call get_appobjs, $(APPL_SECURE_CXXSRCS))
APPL_SECURE_OBJS += $(APPL_SECURE_COBJS) $(APPL_SECURE_ASMOBJS) $(APPL_SECURE_CXXOBJS)


APPL_SECURE_DEPS = $(call get_deps, $(APPL_OBJS))

# compile options only valid in this library
LIB_SECURESHIELD_COMPILE_OPT =


# generate library
SECURESHIELD_SECURE = $(OUT_DIR)/secureshield_secure


##
# Default secure world heap size & stack size
##
DEFAULT_SECURE_HEAPSZ := 1024
DEFAULT_SECURE_STACKSZ := 1024

ifdef SECURE_STACKSZ
	SECURE_STACK_DEFINES := -DSECURE_STACKSIZE=$(strip $(SECURE_STACKSZ))
else
	SECURE_STACK_DEFINES += -DSECURE_STACKSIZE=$(strip $(DEFAULT_SECURE_STACKSZ))
endif


ifdef SECURE_HEAPSZ
	SECURE_HEAP_DEFINES := -DSECURE_HEAPSIZE=$(strip $(SECURE_HEAPSZ))
else
	SECURE_HEAP_DEFINES += -DSECURE_HEAPSIZE=$(strip $(DEFAULT_SECURE_HEAPSZ))
endif

# extra macros to be defined
LIB_SECURESHIELD_DEFINES += -DLIB_SECURESHIELD -DSECURESHIELD_VERSION=$(LIB_SECURESHIELD_VERSION) $(SECURE_HEAP_DEFINES) $(SECURE_STACK_DEFINES)


SECURE_LINK_FILE = $(strip $(OUT_DIR)/secure_linker_$(TOOLCHAIN).ldf)

ifeq ($(VALID_TOOLCHAIN), mw)
SECURE_LINK_OPT	+= $(LCORE_OPT_MW) \
		-Hnocopyr -Hnosdata -Hnocrt -Hldopt=-Coutput=$(SECURESHIELD_SECURE).map \
		-Hldopt=-Csections -Hldopt=-Ccrossfunc -Hldopt=-Csize \
		 -zstdout $(SECURE_LINK_FILE)
else
ifeq ($(VALID_TOOLCHAIN), gnu)
SECURE_LINK_OPT += $(LCORE_OPT_GNU) -mno-sdata -nostartfiles -Wl,-M,-Map=$(SECURESHIELD_SECURE).map -lm -Wl,--script=$(SECURE_LINK_FILE)
endif
endif


ifneq ($(strip $(APPL_SECURE_OBJS)), )
SECURE_SYMBOL_LIST_REQUIRED = 1
ifeq ($(SECURE_SYMBOL_LIST), )
$(error SECURE_SYMBOL_LIST should be given)
endif

ifeq ($(VALID_TOOLCHAIN), mw)
SECURE_LINK_OPT	+=  -Hldopt=-Bsymout=$(SECURESHIELD_SECURE).syms -Hldopt=-Bsymlist=$(SECURE_SYMBOL_LIST)
LINK_OPT += -Hldopt=-Bsymin=$(SECURESHIELD_SECURE).syms

else
ifeq ($(VALID_TOOLCHAIN), gnu)
LINK_OPT += -Xlinker --just-symbols=$(SECURESHIELD_SECURE).syms
endif
endif

endif


ifeq ($(VALID_TOOLCHAIN), mw)
DBG_HW_FLAGS += -cmd="symbols $(SECURESHIELD_SECURE).elf"
else
ifeq ($(VALID_TOOLCHAIN), gnu)
DBG_HW_FLAGS += -ex "add-symbol-file $(SECURESHIELD_SECURE).elf _f_rom_secureshieldruntime -readnow"
endif
endif

SECURE_LIBS += $(OUT_DIR)/arc/startup/arc_cxx_support.o $(OUT_DIR)/arc/arc_cache.o \
		$(SECURE_APPL_LIBS) $(BOARD_LIB) $(MID_LIBS) $(OS_LIBS) $(LIB_LIB_CLIB)

##### SECURE  APPLICATION C/ASM/CPP SOURCE FILE COMPILING RULES#####
.SECONDEXPANSION:
$(APPL_SECURE_COBJS) :$(APPL_OUT_DIR)/%.o: %.c $$(COMMON_COMPILE_PREREQUISITES)
	$(TRACE_COMPILE)
	$(Q)$(CC) -c $(COMPILE_OPT) $< -o $@

.SECONDEXPANSION:
$(APPL_SECURE_CXXOBJS) : $(APPL_OUT_DIR)/%.o: %.cpp $$(COMMON_COMPILE_PREREQUISITES)
	$(TRACE_COMPILE)
	$(Q)$(CXX) -c $(CXX_COMPILE_OPT) $< -o $@

.SECONDEXPANSION:
$(APPL_SECURE_ASMOBJS): $(APPL_OUT_DIR)/%.o: %.s $$(COMMON_COMPILE_PREREQUISITES)
	$(TRACE_ASSEMBLE)
	$(Q)$(CC) -c $(ASM_OPT) $< -o $@


#####RULES FOR GENERATING SECURE LINK FILE FROM TEMPLATE#####
.SECONDEXPANSION:
$(SECURE_LINK_FILE): $(SECURESHIELD_SECUR_LINKER_SCRIPT_FILE) $(SECURESHIELD_BOARD_CONFIG_FILE) $$(COMMON_COMPILE_PREREQUISITES)
	$(TRACE_GEN_LINKFILE)
	$(Q)$(CC) -c $(BOARD_DEFINES) $(APPL_DEFINES) $(LIB_SECURESHIELD_DEFINES) $(LINK_FILE_OPT) $< -o $@


.SECONDEXPANSION:
# library generation rule
$(SECURESHIELD_SECURE).elf: $(LIB_SECURESHIELD_OBJS) $(APPL_SECURE_OBJS) $(SECURE_LIBS) $(SECURE_LINK_FILE) $(SECURE_SYMBOL_LIST)
	$(TRACE_LINK)
	$(Q)$(LD) $(SECURE_LINK_OPT) $(LIB_SECURESHIELD_OBJS) $(APPL_SECURE_OBJS) $(LD_START_GROUPLIB) $(SECURE_LIBS) $(LD_SYSTEMLIBS) $(LD_END_GROUPLIB) -o $@
ifeq ($(SECURE_SYMBOL_LIST_REQUIRED), 1)
ifeq ($(VALID_TOOLCHAIN), gnu)
	$(Q)$(OBJCOPY) -S --keep-symbols $(SECURE_SYMBOL_LIST) $@ $(SECURESHIELD_SECURE).syms
endif
endif

$(SECURESHIELD_SECURE).bin: $(SECURESHIELD_SECURE).elf
	@$(ECHO) "Generating Secure Binary $@"
	$(Q)$(OBJCOPY) $(ELF2BIN_OPT) $< $@


# specific compile rules
# user can add rules to compile this library
# if not rules specified to this library, it will use default compiling rules

.SECONDEXPANSION:
$(LIB_SECURESHIELD_COBJS): $(OUT_DIR)/%.o :$(EMBARC_ROOT)/%.c $$(COMMON_COMPILE_PREREQUISITES)
	$(TRACE_COMPILE)
	$(TRY_MK_OBJDIR)
	$(Q)$(CC) -c $(COMPILE_OPT) -DARC_ENABLE_EXTRA_CALLEE=1 $(LIB_SECURESHIELD_COMPILE_OPT) $< -o $@

.SECONDEXPANSION:
$(LIB_SECURESHIELD_ASMOBJS): $(OUT_DIR)/%.o :$(EMBARC_ROOT)/%.s $$(COMMON_COMPILE_PREREQUISITES)
	$(TRACE_ASSEMBLE)
	$(TRY_MK_OBJDIR)
	$(Q)$(CC) -c $(ASM_OPT) -DARC_ENABLE_EXTRA_CALLEE=1 $(LIB_SECURESHIELD_ASM_OPT) $< -o $@

# Library Definitions
LIB_INCDIR += $(LIB_SECURESHIELD_INCDIR)
LIB_CSRCDIR += $(LIB_SECURESHIELD_CSRCDIR)
LIB_ASMSRCDIR += $(LIB_SECURESHIELD_ASMSRCDIR)

LIB_CSRCS += $(LIB_SECURESHIELD_CSRCS)
LIB_CXXSRCS +=
LIB_ASMSRCS += $(LIB_SECURESHIELD_ASMSRCS)
LIB_ALLSRCS += $(LIB_SECURESHIELD_CSRCS) $(LIB_SECURESHIELD_ASMSRCS)

LIB_COBJS += $(LIB_SECURESHIELD_COBJS) $(APPL_SECURE_OBJS)
LIB_CXXOBJS +=
LIB_ASMOBJS += $(LIB_SECURESHIELD_ASMOBJS)

LIB_DEFINES += $(LIB_SECURESHIELD_DEFINES)
LIB_DEPS += $(LIB_SECURESHIELD_DEPS) $(APPL_SECURE_DEPS)
LIB_LIBS += $(LIB_LIB_SECURESHIELD)

# include secureshield export interface makefile
include $(LIB_SECURESHIELD_DIR)/export/secureshield_export.mk